package com.yls.autoapply.config;

import com.yls.autoapply.model.NameSpace;
import com.yls.autoapply.util.ServiceUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;

import java.util.*;
import java.util.stream.Collectors;

@Configuration
public class ServiceConfig implements EnvironmentAware, InitializingBean {

    private Environment environment;


    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void afterPropertiesSet() throws Exception {
        //读取application.yml，初始化DbProperties
        for (PropertySource<?> propertySource : ((StandardEnvironment) environment).getPropertySources()) {
            if (propertySource instanceof MapPropertySource) {
                if (propertySource.getName().startsWith("applicationConfig")) {
                    LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) propertySource.getSource();
                    List<String> allKey = map.keySet().stream().filter(k -> k.startsWith("autoapply.service.")).collect(Collectors.toList());
                    Map<String, List<String>> mapService = allKey.stream().collect(Collectors.groupingBy(k -> k.split("\\.")[2]));
                    for (String service : mapService.keySet()) {
                        List<NameSpace> list = new ArrayList<>();
                        Map<String, List<String>> mapNameSpace = mapService.get(service).stream().collect(Collectors.groupingBy(k -> k.split("\\.")[3]));
                        for (String namespace: mapNameSpace.keySet()) {
                            NameSpace nameSpace = new NameSpace();
                            nameSpace.setNamespace(namespace);
                            for (String k : mapNameSpace.get(namespace)) {
                                String[] split = k.split("\\.");
                                if ("tagPerfix".equalsIgnoreCase(split[split.length -1])) {
                                    nameSpace.setTagPerfix((String) map.get(k));
                                }
                                if ("file".equalsIgnoreCase(split[split.length -1])) {
                                    nameSpace.setFile((String) map.get(k));
                                }
                                if ("tag".equalsIgnoreCase(split[split.length -1])) {
                                    nameSpace.setTag((String) map.get(k));
                                }
                            }
                            list.add(nameSpace);
                        }
                        ServiceUtil.put(service, list);
                    }
                    for (String key : ServiceUtil.getAllkey()) {
                        System.out.println(key);
                        List<NameSpace> nameSpaces = ServiceUtil.get(key);
                        System.out.println(ServiceUtil.get(key));
                    }
                }
            }
        }



        //System.out.println("配置的数据源" + allKey);

    }



}
